package com.dashu.core.service;

import com.alibaba.nacos.common.util.UuidUtils;
import com.aliyun.oss.OSSClient;
import com.dashu.base.enums.ReplRyCode;
import com.dashu.base.utils.OssClienUtils;
import com.dashu.core.dto.ImgRecordDTO;
import com.dashu.core.handler.VerificationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.xml.bind.ValidationException;
import java.io.InputStream;


/**
 * @Author xys
 * @Date 2020/11/17 15:21
 */

@Slf4j
@Service
public class DocumentService {

    @Value("${dataName.data}")
    private String date;

    public ImgRecordDTO imgRecord(InputStream instream, String fileName) {
        try {
            ImgRecordDTO imgRecordDTO = new ImgRecordDTO();
            String type = fileName.substring(fileName.lastIndexOf("."));
            fileName =UuidUtils.generateUuid() + type;
            System.out.println(fileName);
            String key = OssClienUtils.uploadFileOSS(instream, fileName);
            System.out.println(key);
            String url = OssClienUtils.getUrl1(date+fileName);
            imgRecordDTO.setImgName(fileName);
            imgRecordDTO.setImgUrl(url);
            return imgRecordDTO;
        } catch (Exception e) {
            e.printStackTrace();
            throw new VerificationException(ReplRyCode.DATA_ERROR, ReplRyCode.DATA_ERROR.getMessage());
        }

    }


}
